package br.com.edumobi.fragments;

import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Highlight;

import java.util.ArrayList;

import br.com.edumobi.R;


public class DesempenhoChartHorizontalSimuladoAlunoFragment extends GenericoFragment implements OnChartValueSelectedListener, SeekBar.OnSeekBarChangeListener {

    private TextView textViewTituloSimuladoDesempenhoFragment;
    private HorizontalBarChart horizontalBarChart;
    private SeekBar mSeekBarX, mSeekBarY;
    private TextView tvX, tvY;

    private AcoesDesempenhoChartHorizontalFragment activity;

    private String tituloSimulado;
    private int qtdAcertos;
    private int qtdErros;
    private int qtdNaoRespondidos;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_desempenho_charthorizontal_simulado_aluno, container, false);

        textViewTituloSimuladoDesempenhoFragment = (TextView) view.findViewById(R.id.textViewTituloSimuladoMSResultadoFragment);
        horizontalBarChart = (HorizontalBarChart) view.findViewById(R.id.horizontalChartDesempenhoSimulado);
        mSeekBarX = (SeekBar) view.findViewById(R.id.seekBar1);
        mSeekBarY = (SeekBar) view.findViewById(R.id.seekBar2);
        tvX = (TextView) view.findViewById(R.id.tvXMax);
        tvY = (TextView) view.findViewById(R.id.tvYMax);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (activity != null) {
            activity = (AcoesDesempenhoChartHorizontalFragment) getActivity();

            tituloSimulado = activity.obterSimulado();
            qtdAcertos = activity.obterQtdAcertos();
            qtdErros = activity.obterQtdErros();
            qtdNaoRespondidos = activity.obterQtdNaoRespondidos();
        }

        textViewTituloSimuladoDesempenhoFragment.setText(tituloSimulado);

        carregarHorinzontalChartDesempenhoSimulado();
    }

    @Override
    public void exibirComponentes() {
        textViewTituloSimuladoDesempenhoFragment.setVisibility(View.VISIBLE);
        horizontalBarChart.setVisibility(View.VISIBLE);
        tvX.setVisibility(View.VISIBLE);
        tvY.setVisibility(View.VISIBLE);
        mSeekBarX.setVisibility(View.VISIBLE);
        mSeekBarY.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewTituloSimuladoDesempenhoFragment.setVisibility(View.GONE);
        horizontalBarChart.setVisibility(View.GONE);
        tvX.setVisibility(View.GONE);
        tvY.setVisibility(View.GONE);
        mSeekBarX.setVisibility(View.GONE);
        mSeekBarY.setVisibility(View.GONE);
    }

    private void carregarHorinzontalChartDesempenhoSimulado() {

        horizontalBarChart.setOnChartValueSelectedListener(this);
        horizontalBarChart.setDrawBarShadow(false);
        horizontalBarChart.setDrawValueAboveBar(true);
        horizontalBarChart.setDescription(getResources().getString(R.string.titulo_chart));
        horizontalBarChart.setMaxVisibleValueCount(60);
        horizontalBarChart.setPinchZoom(false);
        horizontalBarChart.setDrawGridBackground(false);

        XAxis xl = horizontalBarChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);
        xl.setDrawAxisLine(true);
        xl.setDrawGridLines(true);
        xl.setGridLineWidth(0.3f);

        YAxis yl = horizontalBarChart.getAxisLeft();
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(true);
        yl.setGridLineWidth(0.3f);

        YAxis yr = horizontalBarChart.getAxisRight();
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(true);

        horizontalBarChart.animateY(2500);

        mSeekBarY.setProgress(50);
        mSeekBarX.setProgress(12);
        mSeekBarY.setOnSeekBarChangeListener(this);
        mSeekBarX.setOnSeekBarChangeListener(this);

        Legend l = horizontalBarChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
        l.setFormSize(8f);
        l.setXEntrySpace(4f);

        ArrayList<String> xVals = new ArrayList<String>();

        xVals.add(tipos[xVals.size() % 5]);

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
        ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();

        yVals1.add(new BarEntry(qtdAcertos * 10, 2));
        yVals2.add(new BarEntry(qtdErros * 10, 1));
        yVals3.add(new BarEntry(qtdNaoRespondidos * 10, 0));

        BarDataSet set1 = new BarDataSet(yVals1, getResources().getString(R.string.nome_acerto));
        set1.setBarSpacePercent(35f);
        set1.setColor(getResources().getColor(R.color.azul_acerto));
        BarDataSet set2 = new BarDataSet(yVals2, getResources().getString(R.string.nome_erro));
        set2.setBarSpacePercent(35f);
        set2.setColor(getResources().getColor(R.color.vermelho_erro));
        BarDataSet set3 = new BarDataSet(yVals3, getResources().getString(R.string.nome_nao_respondida));
        set3.setBarSpacePercent(35f);
        set3.setColor(getResources().getColor(R.color.cinza_nao_respondidos));

        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
        dataSets.add(set1);
        dataSets.add(set2);
        dataSets.add(set3);

        BarData data = new BarData(xVals, dataSets);
        data.setGroupSpace(35f);

        horizontalBarChart.setData(data);
    }

    @Override
    public void onValueSelected(Entry entry, int dataSetIndex, Highlight highlight) {
        if (entry == null)
            return;

        RectF bounds = horizontalBarChart.getBarBounds((BarEntry) entry);
        PointF position = horizontalBarChart.getPosition(entry, horizontalBarChart.getData().getDataSetByIndex(dataSetIndex)
                .getAxisDependency());

        Log.i("bounds", bounds.toString());
        Log.i("position", position.toString());

    }

    @Override
    public void onNothingSelected() {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        tvX.setText(getResources().getString(R.string.quantidade) + (mSeekBarX.getProgress() + 1));
        tvY.setText(getResources().getString(R.string.percentual) + (mSeekBarY.getProgress()));
        horizontalBarChart.invalidate();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    public interface AcoesDesempenhoChartHorizontalFragment extends AcoesGenericoFragment {
        int obterQtdAcertos();
        int obterQtdErros();
        int obterQtdNaoRespondidos();
        String obterSimulado();
    }

    public String[]tipos = new String[]{
      "Java","Itil","PHP","Cobit"
    };
}